package org.rsbot.script.methods;

import java.util.ArrayList;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;

public class ClanChat extends MethodProvider
{
  public static final int INTERFACE_CLAN_CHAT = 589;
  public static final int INTERFACE_CLAN_CHAT_CHANNEL_NAME = 0;
  public static final int INTERFACE_CLAN_CHAT_CHANNEL_OWNER = 1;
  public static final int INTERFACE_CLAN_CHAT_CHANNEL_USERS_LIST = 5;
  public static final int INTERFACE_CLAN_CHAT_BUTTON_JOIN = 11;
  public static final int INTERFACE_JOIN_CLAN_CHAT = 752;
  public static final int INTERFACE_JOIN_CLAN_CHAT_LAST_CHANNEL = 3;
  private String lastChannel = null;

  ClanChat(MethodContext paramMethodContext) {
    super(paramMethodContext);
  }

  public boolean join(String paramString)
  {
    this.methods.game.openTab(11);
    if ((isInChannel()) && 
      (!leave())) {
      return false;
    }

    this.methods.interfaces.getComponent(589, 11).doClick();

    sleep(random(200, 400));
    if (this.methods.interfaces.get(752).isValid()) {
      this.lastChannel = this.methods.interfaces.getComponent(752, 3).getText();

      this.methods.keyboard.sendText(paramString, true);
      sleep(random(1550, 1800));
      if (isInChannel()) {
        this.lastChannel = paramString;
        return true;
      }
    }
    return false;
  }

  public boolean leave()
  {
    this.methods.game.openTab(11);
    if (isInChannel()) {
      this.lastChannel = getChannelOwner();
      this.methods.interfaces.getComponent(589, 11).doClick();

      sleep(random(650, 900));
      return isInChannel();
    }
    return true;
  }

  public String getLastChannel()
  {
    if (isInChannel()) {
      this.lastChannel = getChannelOwner();
    }
    return this.lastChannel;
  }

  public String getChannelOwner()
  {
    String str = this.methods.interfaces.getComponent(589, 1).getText();

    return str.substring(str.lastIndexOf(58) + 2).replaceAll("<.+>", "");
  }

  public String getChannelName()
  {
    String str = this.methods.interfaces.getComponent(589, 0).getText();

    return str.substring(str.lastIndexOf(58) + 2).replaceAll("<.+>", "");
  }

  public String[] getChannelUsers()
  {
    if (isInChannel()) {
      ArrayList localArrayList = new ArrayList();
      RSComponent[] arrayOfRSComponent1 = this.methods.interfaces.getComponent(589, 5).getComponents();

      for (RSComponent localRSComponent : arrayOfRSComponent1) {
        String str1 = localRSComponent.getText();
        if ((str1 == null) || (str1.isEmpty()))
          continue;
        if (localRSComponent.getRelativeX() >= 36)
        {
          continue;
        }
        String str2 = str1.trim();
        if (str2.endsWith("...")) {
          String[] arrayOfString1 = localRSComponent.getActions();
          if (arrayOfString1 != null) {
            for (String str3 : arrayOfString1) {
              if ((str3 != null) && (str3.toLowerCase().matches("^(add|remove)"))) {
                str2 = str3.substring(str3.indexOf(32) + 1);
                str2 = str2.substring(str2.indexOf(32)).trim();
                break;
              }
            }
          }
        }
        localArrayList.add(str2);
      }
      return (String[])localArrayList.toArray(new String[localArrayList.size()]);
    }
    return null;
  }

  public boolean isInChannel()
  {
    String str = this.methods.interfaces.getComponent(589, 11).getText();

    return str.toLowerCase().contains("leave chat");
  }
}